//---------------------------------------------------------------------
// Copyright (C) 2010 - Cobalt Solution / Sebastien Leon              |
//---------------------------------------------------------------------
// This software is under dual licence: commercial licence + GPL v3   |
// To keep closed your modification of this software, you need to     |
// contact me to obtain a commercial licence.                         |
// Any other derivated work should remain Open Source.                |
// Contact me for a licence, a question or if you want to commit      |
// your code to the main tree: seb at cobaltsolutions dot info        |
//---------------------------------------------------------------------

#include "EditLoom.h"
#include "Core.h"
#include "ftpbrowser.h"
#include "FTPThread.h"

#include <QColorDialog>
#include <QMessageBox>

EditLoom::EditLoom(QWidget* parent, Loom& loom, EActionOnLoom action):QDialog(parent),
  mr_loom (loom),
  m_action(action)
{
  ui.setupUi(this);

  // Caption according create or modify...
  setWindowTitle( (action == e_create) ? "Declarer un metier" : (action == e_show) ? "Parametres d'un metier" : "Editer un metier");

  ui.mp_attributs ->setEnabled(action != e_show  );
  ui.mp_deleteJC  ->setEnabled(action != e_show  );
  ui.mp_operations->setEnabled(action != e_create);

  UpdateGui ();
}
//----------------------------------------------------
void EditLoom::UpdateGui()
{
  ui.mp_enabled ->setChecked(mr_loom.IsEnabled ());
  ui.mp_monitor ->setChecked(mr_loom.Monitoring());
  // ui.mp_monitor ->setEnabled();
  ui.mp_name    ->setText   (mr_loom.Name     ());
  ui.mp_address ->setText   (mr_loom.Address  ().toString());
  ui.mp_info1   ->setText   (mr_loom.Info1    ());
  ui.mp_info2   ->setText   (mr_loom.Info2    ());
  ui.mp_posX    ->setValue  (mr_loom.Pos ().x()/Loom::Fc);
  ui.mp_posY    ->setValue  (mr_loom.Pos ().y()/Loom::Fc);
  ui.mp_szWidth ->setValue  (mr_loom.Size().width ()/Loom::Fc);
  ui.mp_szHeight->setValue  (mr_loom.Size().height()/Loom::Fc);
  if (mr_loom.SrcPath().isEmpty()) ui.mp_path->setText("<chemin par defaut>");
                              else ui.mp_path->setText(mr_loom.SrcPath    ()); // (readonly info in this dialog)
  UpdateColor();
  ui.mp_logBrowser->clear();
  QString html("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"><html><head></head><body>");
  // List pending files:
  const QList<FTPThread*>& queue = mr_loom.GetPendingList();
  if (queue.count() > 0)
  {
    html += "<b>Fichiers en attente de transfert:</b><br>";
    for (int m = 0 ; m < queue.count() ; ++m) html += QString(" - %1<br/>").arg(queue[m]->GetTargetName());
    html += "<br><br>Historique:<br>";
  }

  // List LOG entries:
  const QStringList& lst = mr_loom.Log();
  for (int n = 0 ; n < lst.count() ; ++n) html += lst[n] + "<br/>";
  if (!lst.count()) html += "<i>Pas d\'entree d'historique...</i>";
  html += "</body></html>";
  ui.mp_logBrowser->setHtml(html);
}
//----------------------------------------------------
void EditLoom::UpdateColor()
{
  QPixmap color(26,15); color.fill(mr_loom.Color());
  ui.mp_color  ->setIcon(color);
}
//----------------------------------------------------
void EditLoom::accept()
{
  if (m_action != e_show)
  {
    // We update the loom with all current entries
    // (except color which is updated when it is changed)
    mr_loom.SetEnabled   (ui.mp_enabled ->isChecked());
    mr_loom.SetMonitoring(ui.mp_monitor ->isChecked());
    mr_loom.SetName   (ui.mp_name  ->text());
    mr_loom.SetAddress(QHostAddress(ui.mp_address->text()));
    mr_loom.SetInfo1  (ui.mp_info1 ->text());
    mr_loom.SetInfo2  (ui.mp_info2 ->text());
    mr_loom.SetPos    (QPointF(ui.mp_posX   ->value()*Loom::Fc, ui.mp_posY    ->value()*Loom::Fc) );
    mr_loom.SetSize   (QSizeF (ui.mp_szWidth->value()*Loom::Fc, ui.mp_szHeight->value()*Loom::Fc) );
  }
  // Check values:
  if ((m_action == e_show) || !mr_loom.Name().isEmpty() && mr_loom.Address().protocol() == QAbstractSocket::IPv4Protocol)
  {
    QDialog::accept();
  }
  else
  {
    QMessageBox::warning(this, "Declarer ou modifier un metier", "SVP, Verifiez vos parametres.\nLe nom ne doit pas etre vide et l'adresse du controleur\ndoit etre de type IPv4 (par exemple: 192.168.1.220)");
  }
}
//----------------------------------------------------
void EditLoom::selectColor()
{
  Q_ASSERT(m_action != e_show);
  QColor c = QColorDialog::getColor ( mr_loom.Color(), this, "Couleur du metier");
  if (c.isValid())
  {
    mr_loom.SetColor(c);
    UpdateColor();
  }
}
//----------------------------------------------------
void EditLoom::ftpExplore ()
{
  FtpWindow dlg(this, mr_loom);
  dlg.exec();
}
//----------------------------------------------------
void EditLoom::deleteJC   ()
{
  QString msg("Attention, vous allez retirer ce metier des metiers geres par LoomFeed.\nDesirez vous valider cette action ?");
  if (QMessageBox::Ok == QMessageBox::question(this, "Retirer un metier", msg, QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel))
  {
    QDialog::done(e_remove);
  }
}
//----------------------------------------------------
